package com.jzo2o.market.job;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jzo2o.market.enums.ActivityStatusEnum;
import com.jzo2o.market.enums.CouponStatusEnum;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.service.ICouponUseBackService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 优惠劵定时任务
 * @author: jzo2o
 **/
@Component
@Slf4j
public class MarketJob {
    @Resource
    private IActivityService activityService;
    @Resource
    private ICouponService couponService;

    /**
     * 优惠券活动状态定时任务
     * 1.获取全部的优惠券活动
     * 2.判断优惠券活动状态
     */
    @XxlJob("ActivityStatusJob")
    public void ActivityStatusJob() {
        log.info("开始执行优惠券活动状态定时任务");
        ArrayList<Activity> updateList = new ArrayList<>();
        // 获取未生效的优惠券活动,进行状态更新，且开始时间小于当前时间，设置为进行中
        List<Activity> noDistributeactivityList = activityService.list(Wrappers.<Activity>lambdaQuery().eq(Activity::getStatus, ActivityStatusEnum.NO_DISTRIBUTE.getStatus())
                .le(Activity::getDistributeStartTime, LocalDateTime.now()));
        log.info("未生效的需要生效优惠券活动数量：{}", noDistributeactivityList.size());
        if (!noDistributeactivityList.isEmpty()) {
            noDistributeactivityList.forEach(activity -> {
                 //如果活动结束时间小于当前时间，设置为过期
                if (activity.getDistributeEndTime().isBefore(LocalDateTime.now())) {
                    activity.setStatus(ActivityStatusEnum.LOSE_EFFICACY.getStatus());
                    updateList.add(activity);
                } else {
                    activity.setStatus(ActivityStatusEnum.DISTRIBUTING.getStatus());
                    updateList.add(activity);
                }
            });
        }
        //获取进行中的活动
        List<Activity> distributeActivityList = activityService.list(Wrappers.<Activity>lambdaQuery().eq(Activity::getStatus, ActivityStatusEnum.DISTRIBUTING.getStatus())
                .le(Activity::getDistributeEndTime, LocalDateTime.now()));
        log.info("进行中需要失效的活动数量：{}", distributeActivityList.size());
        if (!distributeActivityList.isEmpty()) {
            distributeActivityList.forEach(activity -> {
                activity.setStatus(ActivityStatusEnum.LOSE_EFFICACY.getStatus());
                updateList.add(activity);
            });
        }
        activityService.updateBatchById(updateList);
    }

    /**
     * 已领取优惠券自动过期 每小时一次
     */
    @XxlJob("CouponStatusJob")
    public void CouponStatusJob() {
        log.info("开始执行已领取优惠券自动过期定时任务");
        // 获取所有已领取的优惠券 并且 未使用的优惠券 并关联活动
        List<Coupon> noUsedCouponlist = couponService.list(Wrappers.<Coupon>lambdaQuery().eq(Coupon::getStatus, CouponStatusEnum.NO_USE.getStatus()));
        if (noUsedCouponlist.isEmpty()) {
            log.info("没有需要处理的优惠券");
            return;
        }
        // 判断优惠券是否过期 通过活动截止时间
        ArrayList<Coupon> updateList = new ArrayList<>();
        for (Coupon coupon : noUsedCouponlist) {
            if (coupon.getValidityTime().isBefore(LocalDateTime.now())) {
                coupon.setStatus(CouponStatusEnum.INVALID.getStatus());
                updateList.add(coupon);
            }
        }
        couponService.updateBatchById(updateList);
    }
}
